﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{ 
    public class CoursesController : Controller
    {
        private MainModelContainer db = new MainModelContainer();

        static int id;

        //
        // GET: /Courses/

        public ViewResult Index(bool? edit_course,bool? delete_course,bool? no_point=false)
        {
            if (no_point == true)
            {
                ModelState.AddModelError("", "Сначала создайте хотя бы один пункт");
            }
            if (edit_course == true)
            {
                ViewBag.edit_course = true;
            }
            if (delete_course == true)
            {
                ViewBag.delete_course = true;
            }
            return View(db.Courses.ToList());
        }

        //
        // GET: /Courses/Details/5

        public ViewResult Details(int id)
        {
            Course course = db.Courses.Single(c => c.Id == id);
            return View(course);
        }

        //
        // GET: /Courses/Create

        public ActionResult Create()
        {
            List<CheckPoint> points = (from i in db.CheckPoints select i).Distinct().ToList();
            ViewBag.points_list = new SelectList(points);
            return View();
        } 

        //
        // POST: /Courses/Create

        [HttpPost]
        public ActionResult Create(Course course)
        {
            if (ModelState.IsValid)
            {
                db.Courses.AddObject(course);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(course);
        }
        
        //
        // GET: /Courses/Edit/5
 
        public ActionResult Edit(int id)
        {
            Course course = db.Courses.Single(c => c.Id == id);
            return View(course);
        }

        //
        // POST: /Courses/Edit/5

        [HttpPost]
        public ActionResult Edit(Course course)
        {
            if (ModelState.IsValid)
            {
                db.Courses.Attach(course);
                db.ObjectStateManager.ChangeObjectState(course, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(course);
        }

        //
        // GET: /Courses/Delete/5
 
        public ActionResult Delete(int id)
        {
            Course course = db.Courses.Single(c => c.Id == id);
            return View(course);
        }

        //
        // POST: /Courses/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Course course = db.Courses.Single(c => c.Id == id);
            List<CoursePoint> course_points = (from i in db.CoursePoints where i.CourseRId == course.Id select i).ToList();
            for (int i = 0; i < course_points.Count; i++)
            {
                db.CoursePoints.DeleteObject(course_points.ElementAt(i));
                
            }
            db.SaveChanges();
            List<Ship> ships = (from i in course.Ship select i).ToList();
            for (int i = 0; i < ships.Count; i++)
            {
                db.Ships.DeleteObject(ships.ElementAt(i));                
            }
            db.SaveChanges();
            List<CoursePoint> course_points2 = (from i in db.CoursePoints where i.CourseRId == course.Id select i).ToList();
            db.Courses.DeleteObject(course);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult AddPoint(int course_id)
        {
            id = course_id;
            List<string> ch = (from i in db.CheckPoints select i.Title).Distinct().ToList();
            if (ch.Count == 0)
            {
                return RedirectToAction("Index", new { no_point = true });
            }
            ViewBag.points_list = new SelectList(ch);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddPoint(string points_list)
        {
            CheckPoint ch = (from i in db.CheckPoints where i.Title == points_list select i).FirstOrDefault();
            Course course = (from i in db.Courses where i.Id == id select i).FirstOrDefault();
            CoursePoint course_point = new CoursePoint { CourseRId = course.Id, CheckPointRId = ch.Id,CheckPoint=ch,Course=course };
            db.CoursePoints.AddObject(course_point);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}